package leetcode.easy.hardestworker;

import util.annotation.Solved;

/**
 * <h4>
 *     <a href="https://leetcode.cn/problems/the-employee-that-worked-on-the-longest-task/">
 *         2432. The Employee That Worked on the Longest Task [Easy]
 *     </a>
 * </h4>
 * <h4>How to solve this question?</h4>
 * <p>
 *    One iteration.
 * </p>
 */
@Solved
public class Solution {
    public int hardestWorker(int n, int[][] logs) {
        int id = logs[0][0], longest = logs[0][1];
        for (int i = 1; i < logs.length; i++) {
            int cost = logs[i][1] - logs[i - 1][1];
            if (cost >= longest) {
                id = (cost == longest) ? Math.min(id, logs[i][0]) : logs[i][0];
                longest = cost;
            }
        }
        return id;
    }
}
